package app.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 请求工具类
 * @author zoe
 *
 */
public class ServletUtil {
	
	/**
	 * 获取项目路径
	 * @param request
	 * @return
	 */
	public static String getHomeURL(HttpServletRequest request) {
		return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
	}

	/**
	 * 从request中获取参数
	 * @param request
	 * @return
	 */
	public static Map<String, Object> getParameterMap(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<?, ?> tmap = request.getParameterMap();
		Object[] keys = tmap.keySet().toArray();
		for (int i = 0; i < tmap.size(); i++) {
			String key = keys[i] + "";
			Object value = tmap.get(key);
			if (value == null) {
				map.put(key, null);
			}
			if (!value.getClass().isArray()) {
				map.put(key, value);
			} else {
				String[] arr = (String[]) value;
				if (arr.length == 1) {
					map.put(key, arr[0]);
				} else {
					StringBuffer sb = new StringBuffer();
					for (int k = 0; k < arr.length; k++) {
						if (k != 0) {
							sb.append(",");
						}
						sb.append(arr[k]);
					}
					map.put(key, sb.toString());
				}
			}
		}
		Enumeration<?> e = request.getAttributeNames();
		while (e.hasMoreElements()) {
			String key = (String) e.nextElement();
			Object value = request.getAttribute(key);
			if (value == null) {
				map.put(key, null);
			}
			if (!value.getClass().isArray()) {
				map.put(key, value);
			} else {
				String[] arr = (String[]) value;
				if (arr.length == 1) {
					map.put(key, arr[0]);
				} else {
					StringBuffer sb = new StringBuffer();
					for (int k = 0; k < arr.length; k++) {
						if (k != 0) {
							sb.append(",");
						}
						sb.append(arr[k]);
					}
					map.put(key, sb.toString());
				}
			}
		}
		return map;
	}
	
	/**
	 * 获取IP地址
	 * @return
	 */
	public static String getIpAddress() {
		try {
			InetAddress ip = InetAddress.getLocalHost();
			return ip.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取Mac地址
	 * @return
	 */
	public static String getMacAddress() {
		
        String address = null;
        try {
        	InetAddress ip = InetAddress.getLocalHost();
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            byte[] mac = network.getHardwareAddress();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
            }
            address = sb.toString();

        } catch (SocketException ex) {
            ex.printStackTrace();

        } catch (UnknownHostException e) {
			e.printStackTrace();
		}

        return address;
    }
}
